package org.apache.cordova.notify;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;


import com.mingyi.R;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;


public class NotifyPlugin extends CordovaPlugin {
	
	//private static final Intent MainActivity = null;
	// 通知显示内容
	private PendingIntent pd;

	@SuppressWarnings("deprecation")
	@Override
	public boolean execute(String action, JSONArray args,
			CallbackContext callbackContext) throws JSONException {
		if ("send".equals(action)) {
			//添加通知
			NotificationManager manager = (NotificationManager) this.cordova
					.getActivity().getSystemService(
							Context.NOTIFICATION_SERVICE);
			String title = args.getString(0);
			String text = args.getString(1);
			System.out.println("需要发送的信息..." + text);
			//android6.0之前弃用了setLatestEventInfo，现在.builder里构造
			/*Notification notification = new Notification(R.drawable.icon, text,
					System.currentTimeMillis());*/
			Notification.Builder builder = new Notification.Builder(this.cordova.getActivity());
			builder.setContentTitle(title);
			builder.setContentText(text);
			builder.setWhen(System.currentTimeMillis());
			builder.setAutoCancel(true);//设置可以清除；
			builder.setSmallIcon(R.drawable.icon);

			//页面跳转this.cordova.getActivity().getIntent()
		/*
			Intent m_Intent = new Intent(this.cordova.getActivity(),MainActivity.class);
			 Bundle bundle = new Bundle();
		     bundle.putString("test", "file:///android_asset/www/index.html#notice");
		//   m_Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	   //    m_Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//关键的一步，设置启动模式 
		     m_Intent.putExtras(bundle);
		     System.out.println("m_Intent我的*******"+m_Intent);*/
		     //主要是设置点击通知时显示内容的类 this.cordova.getActivity().getIntent(),PendingIntent.FLAG_CANCEL_CURRENT
			//file:///android_asset/www/index.html#notice
			pd = PendingIntent.getActivity(this.cordova.getActivity(), 0,
					this.cordova.getActivity().getIntent(),PendingIntent.FLAG_UPDATE_CURRENT);
			
			/*
			 * 第二个参数 ：下拉状态栏时显示的消息标题 expanded message title 
			 * 第三个参数：下拉状态栏时显示的消息内容expanded message text , 
			 * 第四个参数：点击该通知时执行页面跳转
			 */
			
			//notification.setLatestEventInfo(this.cordova.getActivity(), title,text, pd);
			// 默认声音
		//	notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
			// 默认振动
			//notification.defaults |= Notification.DEFAULT_VIBRATE;

			Notification notification=builder.getNotification();
			// 通知被点击后，自动消失
			//notification.flags |= Notification.FLAG_AUTO_CANCEL;
			//notification.flags = notification.flags|Notification.FLAG_AUTO_CANCEL ;
		

			// 点击'Clear'时，不清楚该通知(QQ的通知无法清除，就是用的这个)
			//notification.flags |= Notification.FLAG_NO_CLEAR;

			manager.notify(1, notification);

			return true;
		} else {
			return false;
		}
	}
}